var paginationTotal = 20;//记录总数
$(function () {
    $("#confirmDialog").dialog({
        autoOpen:false,
        modal:true,
        show:"slide",
        buttons:{
            "<{$lang.ok}>":function () {
                $(this).dialog("close");
            },
		    "<{$lang.cancel}>":function () {
		        $(this).dialog("close");
		    }
        }
    });
    $("#detailDialog").dialog({
        autoOpen:false,
        modal:true,
        width:700,
        show:"slide",
        buttons:{                           
            "<{$lang.close}>":function () {
                $(this).dialog("close");
            }
        }
    });
    $(".search").click(function () {
        initData(0);
    });
    $(".checkAll").click(function () {
        $(".checkAll").is(":checked") ? $(".checked").attr("checked", true) : $(".checked").attr("checked", false);
    });

    $(".detail").live("click", function () {
        var op_code = $(this).attr("title");
        var html = "<table class='myTab' width='100%' align='center'><tr><td><{$lang.orders_code}></td><td><{$lang.product}><{$lang.sku}></td><td><{$lang.pickup_goods}><{$lang.No}></td><td><{$lang.put_away}><{$lang.code}></td><td><{$lang.batch}></td><td><{$lang.quantity}></td></tr>";

    	loading();
        $.ajax({
            url:"/product/pickup/get-details/pickupCode/" + op_code,
            dataType:"json",
            async:false,
            success:function (json) {
            	closeLoading();
                if (json.ask) {
                    $.each(json.opms, function (k, v) {
                        html += '<tr><td>' + v.orders_code + '</td><td>' + v.product.product_sku + '</td><td>' + v.opm_sort + '</td><td>' + v.batch.putaway_code + '</td><td>' + v.batch.putaway_lot_number + '</td><td>' + v.opm_quantity + '</td></tr>';
                    });
                    html += "</html>";
                    $("#detailDialog").html(html).dialog("open");
                } else {
                    $('<div   title="<{$lang.tip}><{$lang.msg}>"></div>').html('<{$lang.data}><{$lang.exception}>!').dialog({
                        autoOpen:true,
                        modal:true,
                        width:300,
                        show:"slide",
                        buttons:{                    
                		    "<{$lang.close}>":function () {
                		        $(this).dialog("close");
                		    }
                        }
                    });
                }
            }
        });
    });

    $(".confirm").live("click", function () {
        var op_code = $(this).attr("title");
        var html = "<{$lang.confirm}><{$lang.offshelf}><{$lang.code}>"+op_code+"<{$lang.offshelf}>";
        $("#confirmDialog").html(html).dialog({
            autoOpen:true,
            modal:true,
            show:"slide",
            buttons:{
                "<{$lang.ok}>":function () {
    		        $(this).dialog("close");
                	confirmpickup(op_code);
                },
    		    "<{$lang.cancel}>":function () {
    		        $(this).dialog("close");
    		    }
            }
        });
    });
	//回车处理
	$("#pickupCode").add("#orders_code").keyup(function(e){
		var key = e.which;
		if(key==13){
			initData(0);
		}
	});
});
function confirmpickup(op_code) {
	loading();
    $.ajax({
        type:"post",
        async:false,
        dataType:"json",
        url:"/product/pickup/confirm-pickup",
        data:{'pickupCode':op_code},
        success:function (json) {
        	closeLoading();
            if (json.ask == 1) {
                $("#detailDialog").dialog("close");
                initData(0);
            }
            $('<div   title="<{$lang.tip}><{$lang.msg}>"></div>').html(json.msg).dialog({
                autoOpen:true,
                modal:true,
                width:300,
                show:"slide",
                buttons:{                    
        		    "<{$lang.close}>":function () {
        		        $(this).dialog("close");
        		    }
                }
            });
        }
    });
}
function loadData(page, pageSize) {
	loading();
    $.ajax({
        url:"/product/pickup/list/page/" + page + "/pageSize/" + pageSize,
        async:false,
        dataType:"json",
        type:"post",
        data:{
			pickupCode:$("[name=pickupCode]").val(),
			op_status:$("[name=op_status]").val(),
			orders_code:$("[name=orders_code]").val()
		},
        success:function (json) {
        	var html = "";
        	if(!json.ask){
        		paginationTotal = 0;
        		html += '<tr><td colspan="7"><{$lang.no_data}></td></tr>';
        	}else{
        		paginationTotal = json.total;   
				var j = page == 1 ? 1 : pageSize * (page - 1) + 1;				
           		$.each(json.results, function (k, v) {
           			var confirm_html = v.op_status!=1?'<a  href="javascript:;" class="confirm" title="'+v.op_code+'"><{$lang.offshelf}><{$lang.confirm}>'+'</a>&nbsp;&nbsp;':'';
                	html += '<tr><td><input type="checkbox" class="checked" name="orderCode[]" value="' + v.op_code + '"/></td><td>'+(k+1)+'</td><td class="">' + v.op_code + '</td><td>' + v.op_user_name + '</td><td>' + v.statusText + '</td><td>' + v.op_order_cnt + '</td><td>' + v.op_product_cnt + '</td><td>' + v.op_create_date + '</td><td>'+confirm_html+'<a href="javascript:;" class="detail" title="'+v.op_code+'"><{$lang.detail}></a></td></tr>';
            	});            	
        	}
			$("#loadData").html(html);            
        }
    });
    closeLoading();
}